/******************** 
	作用:微信菜单
	作者:蔡俊雄
	版本:V1.0
	时间:2015-07-22
	修改:2015-07-23
	修改:2015-08-12(说明:在添加二级菜单的弹出窗口中 添加为菜单分配功能或自定关键词可以填的功能)
********************/

$(function() {
	//---------------------------配置

	var maxMainMenu = 3; //最多能有多少个一级菜单
	var maxSubMenu = 5; //每个一级菜单最多能有多少个二级菜单
	var listUrl = "/Advanced/WeixinMenu?show=menu"; //获取菜单数据的网址
	var dragUrl = "/Advanced/WeixinMenu/update?rank=1"; //拖动菜单排序的网址
	var deleteUrl = "/Advanced/WeixinMenu/delete"; //删除菜单的网址
	var addMainUrl = "/Advanced/WeixinMenu/add?level=1"; //添加一级菜单的网址
	var editMainUrl = "/Advanced/WeixinMenu/update"; //编辑一级菜单的网址
	var addSubUrl = "/Advanced/WeixinMenu/add?level=2"; //添加二级菜单的网址
	var editSubUrl = "/Advanced/WeixinMenu/update"; //编辑二级菜单的网址
	var publishUrl = "/Advanced/WeixinMenu/post?release=1"; //保存发布菜单的网址

	var currentOperateId = -1; //当前操作的菜单id
	var IDMark_A = "_a";
	var setting = {
		edit: {
			enable: true,
			showRemoveBtn: false,
			showRenameBtn: false
		},
		view: {
			nameIsHTML: true,
			addDiyDom: addDiyDom
		},
		async: {
			enable: true,
			url: listUrl, //,
			type: "get"
				//				autoParam:["id", "name=n", "level=lv"],
				//				otherParam:{"otherParam":"zTreeAsyncTest"},
				//			dataFilter: filter
		},
		callback: {
			//			beforeClick: beforeClick,
			beforeDrag: beforeDrag,
			beforeDrop: beforeDrop,
			beforeAsync: beforeAsync,
			onAsyncError: onAsyncError,
			onAsyncSuccess: onAsyncSuccess
		}
	};

	//扩展对话框标题使其可以支持html格式
	$.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
		_title: function(title) {
			var $title = this.options.title || ' '
			if (("title_html" in this.options) && this.options.title_html == true)
				title.html($title);
			else title.text($title);
		}
	}));

	//---------------------------发布菜单
	/**
	 * 发布菜单
	 */
	function publishMenu() {
		var options = {};
		var url = publishUrl;
		var data = {};
		var tip = "正在发布菜单";
		Common.ajaxPost(options, beforePublish, url, data, tip, completePublish, failPublish);
	};
	//发布菜单前的操作
	function beforePublish() {
		//		General.showMask();
	};
	//发布菜单成功
	function completePublish(options, data, status) {
		//		General.hideMask();
		data = $.parseJSON(data);
		if (data.error)
			General.alert(data.msg);
		else
			General.alert(data.data);
	};
	//发布菜单失败
	function failPublish() {
		//		General.hideMask();
		General.alert("发布菜单失败!");
	};
	//---------------------------删除菜单
	/**
	 * 根据ID删除菜单
	 */
	function deleteById(id) {
		var menuTree = getMenuTree();
		var node = menuTree.getNodeByParam("id", id, null);
		General.confirm("您确定要删除菜单\"" + node.name + "\"吗?", null, startDelete, null, id);
	};
	//开始删除菜单
	function startDelete(id) {
		var options = {};
		var url = deleteUrl;
		var data = {
			"id": id
		};
		var tip = "正在删除菜单";
		Common.ajaxPost(options, beforeDelete, url, data, tip, completeDelete, failDelete);
	};
	//删除菜单前的操作
	function beforeDelete() {
		General.showMask();
	};
	//删除菜单成功
	function completeDelete(options, data, status) {
		data=$.parseJSON(data)
		if (data.error)
			alert(data.msg)
		refresh(); //刷新菜单
	};
	//删除菜单失败
	function failDelete() {
		General.hideMask();
		General.alert("删除菜单失败!");
	};


	//---------------------------添加一级菜单
	//点击"添加一级菜单"按钮
	$("#addMain").on("click", function(e) {
		$("#dialog-edit").html($("#tpl-addMain").html());
		//获取还可以添加多少个一级菜单
		var tree = getMenuTree();
		var nodeList = tree.getNodesByParam("level", 0);
		var remainMainCount = (maxMainMenu - nodeList.length) + "";
		$("#dialog-edit .remainMainCount").html(remainMainCount);
		
		var functionType = 0;
		var functionValue = "";

		//将单选按钮改名
		$("#dialog-edit input[type=radio]").attr("name", "functionType");
		//点击不同的接口类型切换相应的内容
		$("#dialog-edit input[name=functionType]").on("click", function(e) {
			//获取个数
			var index = $(this).attr("data-id");
			index = Number(index);
			$("#dialog-edit .type-content").removeClass("active").eq(index).addClass("active");
		}).eq(functionType).click();
		if (functionType == 1) {
			//关键字
			$("#dialog-edit input[type=text][data-id=keyword]").val(functionValue);
		} else if (functionType == 2) {
			$("#dialog-edit select").val(functionValue);
		} else {
			//默认为打开链接
			$("#dialog-edit input[type=text][data-id=link]").val(functionValue);
		}

		$("#dialog-edit").removeClass('hide').dialog({
			resizable: false,
			width: '560',
			modal: true,
			title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-plus green'></i> 添加一级菜单</h4></div>",
			title_html: true,
			buttons: [{
				html: "<i class='ace-icon fa fa-save'></i> 确定",
				"class": "btn btn-info btn-minier btn-space-right",
				click: function() {
					var result = $("#dialog-edit input[type=text][data-id=subName]").val(); //获取菜单名称
					if (!result) {
						General.alert("请输入一级菜单名称!");
						//						$("#dialog-edit input[type=text][data-id=subName]").focus();
						return;
					}
					functionType = $("#dialog-edit input[type=radio]:checked").val();
					functionType = parseInt(functionType);
					if (isNaN(functionType)) {
						functionType = 1;
					}

					if (functionType == 2) {
						//关键字
						functionValue = $("#dialog-edit input[type=text][data-id=keyword]").val();
					} else if (functionType == 3) {
						functionValue = $("#dialog-edit select").val();
					} else {
						//默认为打开链接
						functionValue = $("#dialog-edit input[type=text][data-id=link]").val();
					}
					var options = {};
					var url = addMainUrl;
					var data = {
						"name": result,
						"functionType": functionType,
						"functionValue": functionValue
					};
					var tip = "正在添加一级菜单";
					Common.ajaxPost(options, beforeAddMain, url, data, tip, completeAddMain, failAddMain);
				}
			}, {
				html: "<i class='ace-icon fa fa-times'></i> 取消",
				"class": "btn btn-danger btn-minier",
				click: function() {
					$(this).dialog("close");
				}
			}]
		});
	});
	//添加一级菜单前的操作
	function beforeAddMain() {
		General.closeEditWindow();
		General.showMask();
	};
	//添加一级菜单成功
	function completeAddMain(options, data, status) {
		
		data=$.parseJSON(data);
		if(data.success){
			General.hideMask();
			alert("添加一级菜单成功!");
		}else{
			General.hideMask();
			if(data.msg)	
				alert(data.msg)
			else
				failAddMain();
		}
		refresh(); //刷新菜单
	};
	//添加一级菜单失败
	function failAddMain() {
		General.alert("添加一级菜单失败!");
	};

	//---------------------------添加二级菜单
	/**
	 * 根据一级菜单id添加二级菜单
	 */
	function showAddDialogById(id) {
		$("#dialog-edit").html($("#tpl-addSub").html());
		//获取还可以添加多少个二级菜单
		var tree = getMenuTree();
		var parentNode = tree.getNodeByParam("id", id, null);
		var nodeList = tree.getNodesByParam("level", 1, parentNode);
		var remainSubCount = (maxSubMenu - nodeList.length) + "";

		var functionType = 0;
		var functionValue = "";

		$("#dialog-edit .remainSubCount").html(remainSubCount);
		//将单选按钮改名
		$("#dialog-edit input[type=radio]").attr("name", "functionType");
		//点击不同的接口类型切换相应的内容
		$("#dialog-edit input[name=functionType]").on("click", function(e) {
			//获取个数
			var index = $(this).attr("data-id");
			index = Number(index);
			$("#dialog-edit .type-content").removeClass("active").eq(index).addClass("active");
		}).eq(functionType).click();
		if (functionType == 1) {
			//关键字
			$("#dialog-edit input[type=text][data-id=keyword]").val(functionValue);
		} else if (functionType == 2) {
			$("#dialog-edit select").val(functionValue);
		} else {
			//默认为打开链接
			$("#dialog-edit input[type=text][data-id=link]").val(functionValue);
		}

		$("#dialog-edit").removeClass('hide').dialog({
			resizable: false,
			width: '560',
			modal: true,
			title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-plus green'></i> 添加二级菜单</h4></div>",
			title_html: true,
			buttons: [{
				html: "<i class='ace-icon fa fa-save'></i> 确定",
				"class": "btn btn-info btn-minier btn-space-right",
				click: function() {
					var result = $("#dialog-edit input[type=text][data-id=subName]").val(); //获取菜单名称
					if (!result) {
						General.alert("请输入二级菜单名称!");
						//						$("#dialog-edit input[type=text][data-id=subName]").focus();
						return;
					}
					functionType = $("#dialog-edit input[type=radio]:checked").val();
					functionType = parseInt(functionType);
					if (isNaN(functionType)) {
						functionType = 1;
					}

					if (functionType == 2) {
						//关键字
						functionValue = $("#dialog-edit input[type=text][data-id=keyword]").val();
					} else if (functionType == 3) {
						functionValue = $("#dialog-edit select").val();
					} else {
						//默认为打开链接
						functionValue = $("#dialog-edit input[type=text][data-id=link]").val();
					}
					var options = {};
					var url = addSubUrl;
					var data = {
						"id": currentOperateId,
						"name": result,
						"functionType": functionType,
						"functionValue": functionValue
					};
					var tip = "正在添加二级菜单";
					Common.ajaxPost(options, beforeAddSub, url, data, tip, completeAddSub, failAddSub);
				}
			}, {
				html: "<i class='ace-icon fa fa-times'></i> 取消",
				"class": "btn btn-danger btn-minier",
				click: function() {
					$(this).dialog("close");
				}
			}]
		});
	};
	//添加二级菜单前的操作
	function beforeAddSub() {
		General.closeEditWindow();
		General.showMask();
	};
	//添加二级菜单成功
	function completeAddSub(options, data, status) {
		data=$.parseJSON(data);
		if(data.success){
			General.hideMask();
			alert("添加二级菜单成功!");
		}else{
			General.hideMask();
			if(data.msg)	
				alert(data.msg)
			else
				failAddSub();
		}
		refresh(); //刷新菜单
	};
	//添加二级菜单失败
	function failAddSub() {
		General.alert("添加二级菜单失败!");
	};
	//---------------------------编辑菜单
	/**
	 * 显示编辑菜单窗口
	 * @param {Number} id 当前编辑的菜单ID
	 * @param {Number} level 当前编辑的菜单层级
	 */
	function showEditDialog(id, level) {
		if (level == 0) {
			//一级菜单
			showEditMainDialog(); //显示一级菜单编辑窗口
		} else {
			//二级菜单
			showEditSubDialog(); //显示二级菜单编辑窗口
		}
	};
	//---------------------------编辑一级菜单
	/**
	 * 显示一级菜单编辑窗口
	 */
	function showEditMainDialog() {
		//先保存id
		var menuTree = getMenuTree();
		var node = menuTree.getNodeByParam("id", currentOperateId, null);
		var name = node.name;
		var functionType = parseInt(node.functionType);
		if (isNaN(functionType)) {
			functionType = 1;
		}
		functionType--;
		var functionValue = node.functionValue;

		$("#dialog-edit").html($("#tpl-editMain").html());
		$("#dialog-edit input[type=text][data-id=subName]").val(name);
		//将单选按钮改名
		$("#dialog-edit input[type=radio]").attr("name", "functionType");
		//点击不同的接口类型切换相应的内容
		$("#dialog-edit input[name=functionType]").on("click", function(e) {
			//获取个数
			var index = $(this).attr("data-id");
			index = Number(index);
			$("#dialog-edit .type-content").removeClass("active").eq(index).addClass("active");
		}).eq(functionType).click();
		if (functionType == 1) {
			//关键字
			$("#dialog-edit input[type=text][data-id=keyword]").val(functionValue);
		} else if (functionType == 2) {
			$("#dialog-edit select").val(functionValue);
		} else {
			//默认为打开链接
			$("#dialog-edit input[type=text][data-id=link]").val(functionValue);
		}
		$("#dialog-edit").removeClass('hide').dialog({
			resizable: false,
			width: '560',
			modal: true,
			title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-edit green'></i> 编辑一级菜单</h4></div>",
			title_html: true,
			buttons: [{
				html: "<i class='ace-icon fa fa-save'></i> 确定",
				"class": "btn btn-info btn-minier btn-space-right",
				click: function() {
					var result = $("#dialog-edit input[type=text][data-id=subName]").val(); //获取菜单名称
					if (!result) {
						General.alert("请输入一级菜单名称!");
						//						$("#dialog-edit input[type=text][data-id=subName]").focus();
						return;
					}
					functionType = $("#dialog-edit input[type=radio]:checked").val();
					functionType = parseInt(functionType);
					if (isNaN(functionType)) {
						functionType = 1;
					}

					if (functionType == 2) {
						//关键字
						functionValue = $("#dialog-edit input[type=text][data-id=keyword]").val();
					} else if (functionType == 3) {
						functionValue = $("#dialog-edit select").val();
					} else {
						//默认为打开链接
						functionValue = $("#dialog-edit input[type=text][data-id=link]").val();
					}
					var options = {};
					var url = editMainUrl;
					var data = {
						"id": currentOperateId,
						"name": result,
						"functionType": functionType,
						"functionValue": functionValue
					};
					var tip = "正在编辑一级菜单";
					Common.ajaxPost(options, beforeEditMain, url, data, tip, completeEditMain, failEditMain);
				}
			}, {
				html: "<i class='ace-icon fa fa-times'></i> 取消",
				"class": "btn btn-danger btn-minier",
				click: function() {
					$(this).dialog("close");
				}
			}]
		});
	};
	//编辑一级菜单前的操作
	function beforeEditMain() {
		General.closeEditWindow();
		General.showMask();
	};
	//编辑一级菜单成功
	function completeEditMain(options, data, status) {
		data=$.parseJSON(data);
		if(data.success){
			General.hideMask();
			alert("编辑菜单成功!");
		}else{
			General.hideMask();
			if (data.msg)
				alert(data.msg);
			else
				failEditSub();			
		}
		refresh(); //刷新菜单
	};
	//编辑一级菜单失败
	function failEditMain() {
		General.alert("编辑一级菜单失败!");
	};
	//---------------------------编辑二级菜单
	/**
	 * 显示二级菜单编辑窗口
	 */
	function showEditSubDialog() {
		//先保存id
		var menuTree = getMenuTree();
		var node = menuTree.getNodeByParam("id", currentOperateId, null);
		var name = node.name;
		var functionType = parseInt(node.functionType);
		if (isNaN(functionType)) {
			functionType = 1;
		}
		functionType--;
		var functionValue = node.functionValue;

		$("#dialog-edit").html($("#tpl-editSub").html());
		$("#dialog-edit input[type=text][data-id=subName]").val(name);
		//将单选按钮改名
		$("#dialog-edit input[type=radio]").attr("name", "functionType");
		//点击不同的接口类型切换相应的内容
		$("#dialog-edit input[name=functionType]").on("click", function(e) {
			//获取个数
			var index = $(this).attr("data-id");
			index = Number(index);
			$("#dialog-edit .type-content").removeClass("active").eq(index).addClass("active");
		}).eq(functionType).click();
		if (functionType == 1) {
			//关键字
			$("#dialog-edit input[type=text][data-id=keyword]").val(functionValue);
		} else if (functionType == 2) {
			$("#dialog-edit select").val(functionValue);
		} else {
			//默认为打开链接
			$("#dialog-edit input[type=text][data-id=link]").val(functionValue);
		}
		$("#dialog-edit").removeClass('hide').dialog({
			resizable: false,
			width: '560',
			modal: true,
			title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-edit green'></i> 编辑二级菜单</h4></div>",
			title_html: true,
			buttons: [{
				html: "<i class='ace-icon fa fa-save'></i> 确定",
				"class": "btn btn-info btn-minier btn-space-right",
				click: function() {
					var result = $("#dialog-edit input[type=text][data-id=subName]").val(); //获取菜单名称
					if (!result) {
						General.alert("请输入二级菜单名称!");
						//						$("#dialog-edit input[type=text][data-id=subName]").focus();
						return;
					}
					functionType = $("#dialog-edit input[type=radio]:checked").val();
					functionType = parseInt(functionType);
					if (isNaN(functionType)) {
						functionType = 1;
					}

					if (functionType == 2) {
						//关键字
						functionValue = $("#dialog-edit input[type=text][data-id=keyword]").val();
					} else if (functionType == 3) {
						functionValue = $("#dialog-edit select").val();
					} else {
						//默认为打开链接
						functionValue = $("#dialog-edit input[type=text][data-id=link]").val();
					}
					var options = {};
					var url = editSubUrl;
					var data = {
						"id": currentOperateId,
						"name": result,
						"functionType": functionType,
						"functionValue": functionValue
					};
					var tip = "正在编辑二级菜单";
					Common.ajaxPost(options, beforeEditSub, url, data, tip, completeEditSub, failEditSub);
				}
			}, {
				html: "<i class='ace-icon fa fa-times'></i> 取消",
				"class": "btn btn-danger btn-minier",
				click: function() {
					$(this).dialog("close");
				}
			}]
		});
	};
	//编辑二级菜单前的操作
	function beforeEditSub() {
		General.closeEditWindow();
		General.showMask();
	};
	//编辑二级菜单成功
	function completeEditSub(options, data, status) {
		data=$.parseJSON(data);
		if(data.success){
			General.hideMask();
			alert("编辑菜单成功!");
		}else{	
			General.hideMask();
			if (data.msg)
				alert(data.msg);
			else
				failEditSub();			
		}
		refresh(); //刷新菜单
	};
	//编辑二级菜单失败
	function failEditSub() {
		General.alert("编辑二级菜单失败!");
	};


	function filter(treeId, parentNode, childNodes) {
		if (!childNodes) return null;
		for (var i = 0, length = childNodes.length; i < length; i++) {
			//			childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
			//			childNodes[i].name = childNodes[i].name+" 添加";
		}
		return childNodes;
	};

	function beforeDrag(treeId, treeNodes) {
		//		for (var i = 0, l = treeNodes.length; i < l; i++) {
		//			if (treeNodes[i].drag === false) {
		//				return false;
		//			}
		//		}
		return true;
	}

	function beforeDrop(treeId, treeNodes, targetNode, moveType) {
		//		console.log("--------------------");
		//		console.log(treeNodes);
		//		console.log(targetNode);
		//		console.log(moveType);

		var node = treeNodes[0];
		var mainCanAdd = canAddMain();
		var childCanAdd = false;
		var dragId = node.id; //被拖动菜单的ID
		var targetId = 0; //目标菜单的ID
		//先判断是否可以拖动,如果可以则向服务器发送数据
		if (targetNode == null) {
			//拖动到树的根里,判断是否二级菜单要变成一级菜单
			if (node.level == 1) {
				//判断一级菜单是否还有位置
				if (mainCanAdd) {
					saveDragMenu(dragId, targetId, moveType);
				} else {
					General.alert("最多可添加" + maxMainMenu + "个一级菜单");
				}
			} else {
				//一级菜单,结果不变

			}
		} else {
			targetId = targetNode.id; //目标菜单的ID
			switch (moveType) {
				case "inner":
					if (targetNode.level == 0) {
						//目标是一级菜单,判断是否还有空位
						childCanAdd = canAddSub(targetNode);
						if (childCanAdd) {
							//目标节点可以添加子菜单
							//判断操作节点是否是二级菜单
							if (node.level == 1) {
								saveDragMenu(dragId, targetId, moveType);
							} else {
								General.alert("只能将二级菜单添加到一级菜单里");
							}
						} else {
							//没有空位添加子菜单
							General.alert("每个一级菜单最多可添加" + maxSubMenu + "个二级菜单");
						}
					} else {
						General.alert("最多只能有二级菜单,不能添加三级菜单");
					}
					break;
				case "prev":
					//只能够在同级之中操作
					if (targetNode.level == 0) {
						//目标是一级菜单,判断是否一级菜单之间的排序
						if (node.level == 0) {
							saveDragMenu(dragId, targetId, moveType);
						} else {
							General.alert("二级菜单不能变成一级菜单");
						}
					} else {
						//目标是二级菜单,判断是否二级菜单之间的排序
						if (node.level == 0) {
							General.alert("一级菜单不能变成二级菜单");
						} else {
							//先判断目标节点和操作节点是否同属于一个父级
							if (node.getParentNode().id == targetNode.getParentNode().id) {
								saveDragMenu(dragId, targetId, moveType);
							} else {
								//判断是否还有空位
								childCanAdd = canAddSub(targetNode.getParentNode());
								if (childCanAdd) {
									saveDragMenu(dragId, targetId, moveType);
								} else {
									//没有空位添加子菜单
									General.alert("每个一级菜单最多可添加" + maxSubMenu + "个二级菜单");
								}
							}
						}
					}
					break;
				case "next":
					//只能够在同级之中操作
					if (targetNode.level == 0) {
						//目标是一级菜单,判断是否一级菜单之间的排序
						if (node.level == 0) {
							saveDragMenu(dragId, targetId, moveType);
						} else {
							General.alert("二级菜单不能变成一级菜单");
						}
					} else {
						//目标是二级菜单,判断是否二级菜单之间的排序
						if (node.level == 0) {
							General.alert("一级菜单不能变成二级菜单");
						} else {
							//先判断目标节点和操作节点是否同属于一个父级
							if (node.getParentNode().id == targetNode.getParentNode().id) {
								saveDragMenu(dragId, targetId, moveType);
							} else {
								//判断是否还有空位
								childCanAdd = canAddSub(targetNode.getParentNode());
								if (childCanAdd) {
									saveDragMenu(dragId, targetId, moveType);
								} else {
									//没有空位添加子菜单
									General.alert("每个一级菜单最多可添加" + maxSubMenu + "个二级菜单");
								}
							}
						}
					}
					break;
				default:
					break;
			}

		}
		return false;
	};
	//---------------------------拖动菜单
	/**
	 * 保存菜单拖动数据
	 * @param {Number} dragId 被拖动菜单的ID
	 * @param {Number} targetId 目标菜单的ID
	 * @param {String} type 拖动类型(prev,next,inner)
	 */
	function saveDragMenu(dragId, targetId, type) {
		var options = {};
		var url = dragUrl;
		var data = {
			"dragId": dragId,
			"targetId": targetId,
			"type": type
		};
		var tip = "正在保存菜单拖动数据";
		Common.ajaxPost(options, beforeSaveDragMenu, url, data, tip, completeSaveDragMenu, failSaveDragMenu);
	};
	//保存菜单拖动数据前的操作
	function beforeSaveDragMenu() {
		General.showMask();
	};
	//保存菜单拖动数据成功
	function completeSaveDragMenu(options, data, status) {
		data=$.parseJSON(data);
		if(data.success){
			General.hideMask();
			alert("重新排序成功!");
		}else{			
			failSaveDragMenu();			
		}
		refresh(); //刷新菜单
	};
	//保存菜单拖动数据失败
	function failSaveDragMenu() {
		General.hideMask();
		General.alert("保存菜单拖动数据失败!");
	};

	//展开一级菜单时不要去服务器获取数据
	function beforeAsync(treeId, treeNode) {
		if (!!treeNode && !!treeNode.name) {
			return false;
		}
		return true;
	};
	//获取菜单数据失败
	function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
		General.alert("获取菜单数据失败,请检查网络链接!");
	}

	//菜单加载成功
	function onAsyncSuccess(event, treeId, treeNode, msg) {
		getMenuTree().expandAll(true); //展开菜单
		$("#save,#preview").removeAttr("disabled");
		General.hideMask();
		checkAddMain(); //检查是否可以启用"添加一级菜单"按钮
	};
	//为菜单添加相应的按钮
	function addDiyDom(treeId, treeNode) {
		if (treeNode.parentNode && treeNode.parentNode.id != 2) return;
		var aObj = $("#" + treeNode.tId + IDMark_A);
		var addStr = "";
		var editStr = "";
		var hasAdd = treeNode.level == 0 && canAddSub(treeNode);
		var deleteStr = "<span class='operation-icon' id='operationDelete" + treeNode.id + "' title='" + "删除" + "' onfocus='this.blur();'><span class='button operation-icon-delete'></span></span>";
		if (hasAdd) {
			addStr = "<span class='operation-icon' id='operationAdd" + treeNode.id + "' title='" + "添加二级菜单" + "' onfocus='this.blur();'><span class='button operation-icon-add'></span></span>";
		}
		editStr = "<span class='operation-icon' id='operationEdit" + treeNode.id + "' title='" + "编辑" + "' onfocus='this.blur();'><span class='button operation-icon-edit'></span></span>";
		var str = addStr + editStr + deleteStr;
		aObj.append(str);
		if (hasAdd) {
			var addButton = $("#operationAdd" + treeNode.id);
			addButton.bind("click", function() {
				currentOperateId = treeNode.id;
				showAddDialogById(treeNode.id);
				return false;
			});
		}
		var editButton = $("#operationEdit" + treeNode.id);
		editButton.bind("click", function() {
			currentOperateId = treeNode.id;
			showEditDialog(treeNode.id, treeNode.level);
			return false;
		});
		var deleteButton = $("#operationDelete" + treeNode.id);
		deleteButton.bind("click", function() {
			currentOperateId = treeNode.id;
			deleteById(treeNode.id);
			return false;
		});
	};


	/**
	 * 获取菜单数据
	 */
	function getMenuTree() {
		return $.fn.zTree.getZTreeObj("menuTree");
	};

	//刷新菜单
	function refresh() {
		getMenuTree().reAsyncChildNodes(null, "refresh", false); //重新加载数据
	};

	/**
	 * 检查是否可以启用"添加一级菜单"按钮
	 */
	function checkAddMain() {
		if (canAddMain()) {
			//启用按钮
			$("#addMain").removeAttr("disabled");
		} else {
			//禁用按钮
			$("#addMain").attr("disabled", "disabled");
		}
		//		console.log(nodeList.length);
		//		var treeObj = getMenuTree();
		//获取一级菜单个数
		//		var nodes = treeObj.transformToArray(treeObj.getNodes());
		//		Common.log(treeObj.getNodes());
		//		Common.log(nodes);
	};

	/**
	 * 检查是否可以添加一级菜单
	 */
	function canAddMain() {
		var tree = getMenuTree();
		var nodeList = tree.getNodesByParam("level", 0);
		return (nodeList.length < maxMainMenu);
	};

	/**
	 * 检查当前的一级菜单下是否可以添加二级菜单
	 */
	function canAddSub(node) {
		var tree = getMenuTree();
		var nodeList = tree.getNodesByParam("level", 1, node);
		return (nodeList.length < maxSubMenu);
	};

	/**
	 * 初始化
	 */
	function init() {
		$("#publish").on("click", function(e) {
			publishMenu(); //发布菜单
		});
		initButtons(); //一开始时禁用按钮
		$.fn.zTree.init($("#menuTree"), setting);
	};
	/**
	 * 一开始时禁用按钮
	 */
	function initButtons() {
		$("#addMain,#save,#preview").attr("disabled", "disabled");
	};

	init(); //初始化

	//	General.showDialogLoading({disableButton:".btn-space-right"});
	//	$("#dialog-edit button").attr("disabled","disabled");
	//	General.hideDialogLoading();
	//	General.showDialogLoading();
});
